//车辆定位

interface ICarLocationViewModel extends ng.IScope {
    
    /**
     * 定位日期
     */
    creationTime:string;
    /**
     * 定位情况
     */
    statusCode:string;
    /**
     * 位置信息
     */
    area:string;
    /**
     * 历史定位位置信息
     */
    areaS:string;
    /**
     * 历史定位时间
     */
    creationTimeS:string;
    /**
     * 是否启用注册按钮
     */
    
    isRegisterEnabled: boolean;
    /**
     * 是否启用定位按钮
     */
    isLocationEnabled: boolean;
    /**
     * 起始地
     */
    shipAddress: string;
    /**
     * 目的地
     */
    deliverAddress: string;
    /**
     * 定位地址
     */
    locationAddress: string;
    /**
     * 车牌号
     */
    carCode: string;
    /**
     * 手机号
     */
    phoneNumber: string;
    /**
     * 状态提示
     */
    statusText: string;
    /**
     * 注册事件
     */
    register: () => void;
    /**
     * 定位事件
     */
    location: () => void;
    /**
     * 定位按钮文字
     */
    locationText: string;
    /**
     * 返回事件
     */
    goback:()=>void;
    /**
     * 开通定位
     */
    dredge:()=>void;
}

let markerArr:any[];
class CarLocationController {
    carId: string;
    map: any;
    myGeo:any;
    adds:any[];
    constructor(private $scope: ICarLocationViewModel, private locationService: ILocationService, private $location: ng.ILocationService,
        private $ngBootbox: BootboxService) {
        this.init();
    }

    init(): void {
        
        this.$scope.locationText="定位";
        this.$scope.location=this.location;

        this.carId = this.$location.search().id;
        this.$scope.carCode = this.$location.search().carcode;
        this.$scope.phoneNumber = this.$location.search().phone;
        let shipAddress=this.$scope.shipAddress = this.$location.search().shipAddress;
        let deliverAddress=this.$scope.deliverAddress = this.$location.search().deliverAddress;

        this.adds=[shipAddress,deliverAddress]

        this.$scope.isLocationEnabled = true;
        this.$scope.isRegisterEnabled = true;
        this.myGeo = new BMap.Geocoder();

        this.initMap();
        this.history_location(this.carId,true);
        this.getLoctionState(this.carId,true);

        
        this.$scope.location = this.location;
        this.$scope.dredge=this.dredge;
        this.$scope.goback = ()=> {
            window.history.go(-1);
        }
    }

     //创建和初始化地图函数：
        initMap() {
            this.createMap(); //创建地图
            this.setMapEvent(); //设置地图事件
            this.addMapControl(); //向地图添加控件
        }

        //创建地图函数：
        createMap() {
            
            this.map = new BMap.Map("dituContent"); //在百度地图容器中创建一个地图
            var point = new BMap.Point(119.446448, 32.164418); //定义一个中心点坐标
            this.map.centerAndZoom(point, 7); //设定地图的中心点和坐标并将地图显示在地图容器中
            // window['map'] = this.map; //将map变量存储在全局
        }

        //地图事件设置函数：
        setMapEvent() {
            this.map.enableScrollWheelZoom(); //启用地图滚轮放大缩小
        }

        //地图控件添加函数：
        addMapControl() {
            //向地图中添加缩放控件
            var ctrl_nav = new BMap.NavigationControl({
                anchor: BMAP_ANCHOR_TOP_LEFT,
                type: BMAP_NAVIGATION_CONTROL_LARGE
            });
            this.map.addControl(ctrl_nav);
            //向地图中添加比例尺控件
            var ctrl_sca = new BMap.ScaleControl({
                anchor: BMAP_ANCHOR_BOTTOM_LEFT
            });
            this.map.addControl(ctrl_sca);
        }

        //创建InfoWindow
        createInfoWindow(i) {
            var json = markerArr[i];
            var iw = new BMap.InfoWindow("<b class='iw_poi_title' title='" + json.title + "'>" + json.title + "</b><div class='iw_poi_content'>" + json.content + "</div>");
            return iw;
        }





        // 编写创建标注函数
        addMarker(point, label) {
            var marker = new BMap.Marker(point);
            this.map.addOverlay(marker);
            marker.setLabel(label);
        }

        //逆地址解析函数,起始点使用
        s_e(adds) {
            this.myGeo.getPoint(adds, (point)=> {
                if (point) {
                    var address = new BMap.Point(point.lng, point.lat);
                    this.addMarker(address, new BMap.Label(adds, {
                        offset: new BMap.Size(20, -10)
                    }));
                }
            });
        }

        //判断开通注册状态
        getLoctionState(carid, isVehicle):void {
            this.locationService.getLocationState(carid, isVehicle).then(
                (res)=> {
                    if (res.data.isRegister == true) {
                        this.$scope.isRegisterEnabled = true;
                        this.$scope.isLocationEnabled = false;

                    } else {
                        this.$scope.isRegisterEnabled = false;
                        this.$scope.isLocationEnabled = true;
                    }
                },
                (rej)=> {
                    this.$scope.isRegisterEnabled = true;
                    this.$scope.isLocationEnabled = true;
                    console.log(rej);
                }
            )
        }

        //历史定位
        history_location(carid, isVehicle) {
            this.locationService.getHistoryLocation(carid, isVehicle).then(
                (resq) =>{
                    this.$scope.areaS = resq.data.area;
                    this.$scope.creationTimeS = resq.data.creationTime;
                },
                (rel)=> {
                    console.log(rel)
                }
            );
        }

        //点击定位事件,必须是已开通
        location=() =>{
            var carid=this.carId;
            var isVehicle=true;
            this.$scope.locationText = "定位中，请稍等...";
            this.$scope.isLocationEnabled = true;
            this.$scope.creationTime = "";
            this.$scope.statusCode = "";
            this.$scope.area = "";
            this.initMap();
            for (var i = 0; i < this.adds.length; ++i) {
                this.s_e(this.adds[i]);
            }
            this.locationService.getLocation(carid, isVehicle).then(
                (resq)=> {
                    this.$scope.area = resq.data.area;
                    this.$scope.creationTime = resq.data.creationTime;
                    this.$scope.statusCode = "定位成功";

                    // 百度API
                    var p0 = resq.data.longitude;
                    var p1 = resq.data.latitude;
                    var pt = new BMap.Point(p0, p1);
                    this.addMarker(pt, new BMap.Label(this.$scope.area, { offset: new BMap.Size(20, -10) }));
                    this.$scope.locationText = "定位";
                    this.$scope.isRegisterEnabled = true;
                },

                (resl) =>{
                    this.$scope.statusCode = "定位失败";
                    this.$scope.locationText = "定位";
                    this.$scope.isRegisterEnabled = true;
                    this.$scope.isLocationEnabled = false;
                }
            );
            this.history_location(carid, isVehicle);
        }

        //点击开通注册功能，必须是未开通
        dredge =()=> {
            let carid=this.carId;
            let isVehicle=true;
            this.$scope.isRegisterEnabled = true;
            this.locationService.getDredge(carid, isVehicle).then(
                (res) =>{
                    if (res.data.isRegister == true) {
                        alert('驾车司机短信同意开通才能使用定位！,同意后刷新一次页面！')
                    } else {
                        this.$scope.isRegisterEnabled = false;
                    }
                },
                (rel) =>{
                    alert('无法开通,请返回');
                })
        }

       
     
     
}

angular.module("tms").controller("carLocationController",["$scope","locationService","$location","$ngBootbox",CarLocationController]);